package com.ronx.scs.controller;

import com.ronx.scs.model.Registry;
import com.ronx.scs.model.User;
import com.ronx.scs.service.RegistryService;
import com.ronx.scs.service.UserService;
import com.ronx.scs.validator.UserValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Produces;
import java.util.Date;

@Controller
@Produces("application/xml")
//@RequestMapping("/registry")
public class ServletController {

//    @Autowired
//    private FreeMarkerView freeMarkerView;

    @Autowired
    private RegistryService registryService;
    @Autowired
    private UserService userService;

    @Autowired
    private UserValidator registryValidator;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView redirect_main() {
        ModelAndView modelAndView = new ModelAndView("start");
        return modelAndView;
//        File file = new File("");
//        try (InputStream is = new FileInputStream(file)) {
//            is.read();
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
//    @ResponseBody
    public Registry findEmployee(@PathVariable("id") int registryId) {
        return registryService.findById(registryId);
    }

    @RequestMapping(value = "/entry", method = RequestMethod.POST)
    public String add(@ModelAttribute("user") User user, BindingResult result) {
//        user.set(new Date());
//        registryValidator.validate(registry, result);
//        if (result.hasErrors())
//            return "error";
        userService.add(user);
        return "redirect:entry";
    }

    @RequestMapping(value = "/entry", method = RequestMethod.GET)
//    @ResponseBody
    public ModelAndView entry(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        ModelAndView modelAndView = new ModelAndView("entry");
        modelAndView.addObject("registry", new Registry());
        return modelAndView;
    }

    @RequestMapping(value = "/stat", method = RequestMethod.GET)
    public ModelAndView stat(@ModelAttribute("registry") Registry registry) throws Exception {
        ModelAndView modelAndView = new ModelAndView("result");
//        List<Registry> registryList = registryService.getByNumber(registry.getNumber());
//        modelAndView.addObject("registryList", registryList);
        modelAndView.addObject("currentDate", new Date());
//        modelAndView.addObject("owner", new Owner());
        return modelAndView;
    }

    @RequestMapping(value = "/main", method = RequestMethod.GET)
    public ModelAndView main(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        ModelAndView modelAndView = new ModelAndView("main");
        return modelAndView;
    }

    @RequestMapping(value = "/exception", method = RequestMethod.GET)
    public ModelAndView exception(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        ModelAndView modelAndView = new ModelAndView("exception");
        return modelAndView;
    }

}